package com.zhou.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonValue;
import com.zhou.config.Result;
import com.zhou.entity.QuesCategory;
import com.zhou.entity.QuestionRepo;
import com.zhou.entity.TheoQues;
import com.zhou.entity.TheoQuesAns;
import com.zhou.enums.CreatorEnum;
import com.zhou.enums.IsRightEnum;
import com.zhou.enums.LevelEnum;
import com.zhou.enums.QuesTypeEnum;
import com.zhou.mapper.QuestionRepoMapper;
import com.zhou.mapper.TheoQuesAnsMapper;
import com.zhou.mapper.TheoQuesMapper;
import com.zhou.service.TheoQuesAnsService;
import com.zhou.service.TheoQuesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 题目信息表 前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-03-02
 */
@RestController
@RequestMapping("/question")
@Api(tags = "理论题表接口信息")
public class TheoQuesController {

    @Autowired
    private TheoQuesMapper theoQuesMapper;

    @Autowired
    private TheoQuesAnsMapper theoQuesAnsMapper;

    @Autowired
    private QuestionRepoMapper questionRepoMapper;

    @Autowired
    private TheoQuesService theoQuesService;

    private static final Integer SIZE = 100;

    @ApiOperation(value = "得到“方向”")
    @GetMapping("/findDirect")
    public List<String> findDirect(@RequestParam(name = "repo_name",required = false)String repo_name){
        if(repo_name == null){
            return this.theoQuesMapper.findDirect();
        }
        else
        {
            int idByName = questionRepoMapper.getIdByName(repo_name);
            return this.theoQuesMapper.findDirectByRepoId(idByName);
        }

    }

    @ApiOperation(value = "得到“知识点”")
    @GetMapping("/findKnowledge")
    public List<String> findKnowledge(){
        return this.theoQuesMapper.findKnowledge();
    }

    @ApiOperation(value = "得到“题目类型”")
    @GetMapping("/findQuesType")
    public List<String> findQuesType(){
        List<String> list = theoQuesMapper.findQuesType();
        List<String> quesType = new ArrayList<>();
        for(int i = 0; i < list.size();i++)
        {
            String name = "";
            if(list.get(i).equals("0")){
                name = "SINGLE_CHOICE";
            }
            else if(list.get(i).equals("1")){
                name = "DOZEN_CHOICE";
            }
            else if(list.get(i).equals("2")){
                name = "JUDGE";
            }
            else{
                name = "FILL_BLANK";
            }
            quesType.add(QuesTypeEnum.valueOf(name).getDisplayName());
        }
        return quesType;
    }

    @ApiOperation(value = "得到“难度等级")
    @GetMapping("/findLevel")
    public List<String> findLevel(){
        List<String>list = theoQuesMapper.findLevel();
        List<String> level = new ArrayList<>();
        for(int i = 0; i < list.size();i++)
        {
            String name = "";
            if(list.get(i).equals("0")){
                name = "EASY";
            }
            else if(list.get(i).equals("1")){
                name = "MEDIUM";
            }
            else{
                name = "HARD";
            }
            level.add(LevelEnum.valueOf(name).getDisplayName());
        }
        return level;
    }

//    @ApiOperation(value = "根据“方向、知识点、题型、难度”显示题目")
//    @GetMapping("/findQuesByThree/{page}")
//    public Page<TheoQues> findQues(@RequestParam(name = "question_type",required = false)String question_type,
//                         @RequestParam(name = "knowledge",required = false)String knowledge,
//                         @RequestParam(name = "level",required = false)String level,
//                                   @RequestParam(name = "direct",required = false)String direct,
//                                   @PathVariable Integer page
//                        ){
//        Page<TheoQues> searchPage = new Page<>(page,SIZE);
//        QueryWrapper<TheoQues> queryWrapper = new QueryWrapper<>();
//
//        if(question_type == null && knowledge == null && level == null &&direct == null){
//            return this.theoQuesMapper.selectPage(searchPage,queryWrapper);
//        }
//        else {
//            if(question_type != null && knowledge != null && level != null && direct!=null) {
//                queryWrapper.like("direct",direct)
//                        .like("knowledge",knowledge)
//                        .like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
//                        .like("level",LevelEnum.valueOf(level).getValue());
//            }
//            else if(question_type != null && knowledge == null && level == null && direct ==null){
//                queryWrapper .like("question_type",QuesTypeEnum.valueOf(question_type).getValue());
//            }
//
//            else if(question_type == null && knowledge != null && level == null && direct==null){
//                queryWrapper.like("knowledge",knowledge);
//            }
//
//            else if(question_type == null && knowledge == null && level != null&& direct ==null){
//                queryWrapper.like("level",LevelEnum.valueOf(level).getValue());
//            }
//
//            else if(question_type == null && knowledge == null && level == null&& direct !=null){
//                queryWrapper.like("direct",direct);
//            }
//
//            else if(question_type != null && knowledge != null && level == null && direct==null){
//                queryWrapper.like("knowledge",knowledge)
//                        .like("question_type",QuesTypeEnum.valueOf(question_type).getValue());
//            }
//
//            else if(question_type != null && knowledge == null && level != null && direct==null){
//                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
//                        .like("level",LevelEnum.valueOf(level).getValue());
//            }
//            else if(question_type != null && knowledge == null && level == null && direct!=null){
//                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
//                        .like("direct",direct);
//            }
//
//            else if(question_type == null && knowledge != null && level != null && direct==null){
//                queryWrapper.like("knowledge",knowledge)
//                        .like("level",LevelEnum.valueOf(level).getValue());
//            }
//            else if(question_type == null && knowledge != null && level == null && direct!=null){
//                queryWrapper.like("knowledge",knowledge)
//                        .like("direct",direct);
//            }
//            else if(question_type == null && knowledge == null && level != null && direct!=null){
//                queryWrapper.like("level",LevelEnum.valueOf(level).getValue())
//                        .like("direct",direct);
//            }
//            else if(question_type != null && knowledge != null && level != null && direct==null){
//                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
//                        .like("level",LevelEnum.valueOf(level).getValue())
//                        .like("knowledge",knowledge);
//            }
//            else if(question_type != null && knowledge != null && level == null && direct!=null){
//                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
//                        .like("direct",direct)
//                        .like("knowledge",knowledge);
//            }
//            else {
//                queryWrapper.like("level",LevelEnum.valueOf(level).getValue())
//                        .like("direct",direct)
//                        .like("knowledge",knowledge);
//            }
//
//            }
//            return this.theoQuesMapper.selectPage(searchPage,queryWrapper);
//        }

    @ApiOperation(value = "根据“方向、题库名称、题型、难度”显示题目")
    @GetMapping("/findQuesByThree/{page}")
    public Page<TheoQues> findQues(@RequestParam(name = "question_type",required = false)String question_type,
                                   @RequestParam(name = "direct",required = false)String direct,
                                   @RequestParam(name = "level",required = false)String level,
                                   @RequestParam(name = "repo_name",required = false)String repo_name,
                                   @PathVariable Integer page
    ){
        Page<TheoQues> searchPage = new Page<>(page,SIZE);
        QueryWrapper<TheoQues> queryWrapper = new QueryWrapper<>();

        if(question_type == null && direct == null && level == null &&repo_name == null){
            return this.theoQuesMapper.selectPage(searchPage,queryWrapper);
        }
        else {
            if(question_type != null && direct != null && level != null && repo_name!=null) {
                queryWrapper.like("ques_repo_id",questionRepoMapper.getIdByName(repo_name))
                        .like("direct",direct)
                        .like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
                        .like("level",LevelEnum.valueOf(level).getValue());
            }
            else if(question_type != null && direct == null && level == null && repo_name ==null){
                queryWrapper .like("question_type",QuesTypeEnum.valueOf(question_type).getValue());
            }

            else if(question_type == null && direct != null && level == null && repo_name==null){
                queryWrapper.like("direct",direct);
            }

            else if(question_type == null && direct == null && level != null&& repo_name ==null){
                queryWrapper.like("level",LevelEnum.valueOf(level).getValue());
            }

            else if(question_type == null && direct == null && level == null&& repo_name !=null){
                queryWrapper.like("ques_repo_id",questionRepoMapper.getIdByName(repo_name));
            }

            else if(question_type != null && direct != null && level == null && repo_name==null){
                queryWrapper.like("direct",direct)
                        .like("question_type",QuesTypeEnum.valueOf(question_type).getValue());
            }

            else if(question_type != null && direct == null && level != null && repo_name==null){
                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
                        .like("level",LevelEnum.valueOf(level).getValue());
            }
            else if(question_type != null && direct == null && level == null && repo_name!=null){
                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
                        .like("ques_repo_id",questionRepoMapper.getIdByName(repo_name));
            }

            else if(question_type == null && direct != null && level != null && repo_name==null){
                queryWrapper.like("direct",direct)
                        .like("level",LevelEnum.valueOf(level).getValue());
            }
            else if(question_type == null && direct != null && level == null && repo_name!=null){
                queryWrapper.like("direct",direct)
                        .like("ques_repo_id",questionRepoMapper.getIdByName(repo_name));
            }
            else if(question_type == null && direct == null && level != null && repo_name!=null){
                queryWrapper.like("level",LevelEnum.valueOf(level).getValue())
                        .like("ques_repo_id",questionRepoMapper.getIdByName(repo_name));
            }
            else if(question_type != null && direct != null && level != null && repo_name==null){
                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
                        .like("level",LevelEnum.valueOf(level).getValue())
                        .like("direct",direct);
            }
            else if(question_type != null && direct != null && level == null && repo_name!=null){
                queryWrapper.like("question_type",QuesTypeEnum.valueOf(question_type).getValue())
                        .like("ques_repo_id",questionRepoMapper.getIdByName(repo_name))
                        .like("direct",direct);
            }
            else {
                queryWrapper.like("level",LevelEnum.valueOf(level).getValue())
                        .like("ques_repo_id",questionRepoMapper.getIdByName(repo_name))
                        .like("direct",direct);
            }

        }
        return this.theoQuesMapper.selectPage(searchPage,queryWrapper);
    }

    @ApiOperation(value = "搜索引擎，根据题目内容查询数据")
    @GetMapping("/searchCon/{page}")
    public Page<TheoQues> searchByCon(@RequestParam(name = "contend",required = false) String contend,
                                      @PathVariable Integer page){
        QueryWrapper<TheoQues>queryWrapper = new QueryWrapper<>();
        queryWrapper.like("contend",contend);
        Page<TheoQues> pageSearch = new Page<>(page,SIZE);
        return theoQuesMapper.selectPage(pageSearch,queryWrapper);
    }

    @ApiOperation(value = "根据方向呈现知识点")
    @GetMapping("/searchKnowledge")
    public List<String> searchKnowledge(@RequestParam(name = "direct")String direct){
        return this.theoQuesMapper.findKnowledgeByDirect(direct);
    }

    @ApiOperation(value = "呈现此表的所有信息")
    @GetMapping("/findAllQues")
    public List<TheoQues> findAllQues(){
        return this.theoQuesMapper.selectList(null);
    }



    @ApiOperation(value = "新增一条选择题/判断题")
    @PutMapping("/addQues")
    public String addQues(@RequestParam(name = "choiceId") String []quesChoices,
                          @RequestParam(name = "ans") int[]isRight,
                          @RequestBody TheoQues theoQues){
        theoQuesMapper.insert(theoQues);
        int quesId = theoQues.getQuesId();
        for(int i = 0;i < isRight.length;i++){
            TheoQuesAns theoQuesAns = new TheoQuesAns();
            theoQuesAns.setQuesId(quesId);
            theoQuesAns.setContend(quesChoices[i]);
            if(isRight[i] == 0){
                theoQuesAns.setIsRight(IsRightEnum.WRONG);
            }
            else{
                theoQuesAns.setIsRight(IsRightEnum.RIGHT);
            }
            theoQuesAnsMapper.insert(theoQuesAns);
        }
        return "增加题目成功";
    }


    @ApiOperation(value = "修改一条题目信息")
    @PostMapping("/updateQues")
    public boolean update(@RequestBody TheoQues theoQues){
        return this.theoQuesService.saveOrUpdate(theoQues);
    }

    @ApiOperation(value = "根据ques_id进行批量删除")
    @PostMapping("/batch-delete")
    public ResponseEntity<?> deleteBatch(@RequestBody List<Integer> quesIds) {
        boolean success = theoQuesService.removeBatchByIds(quesIds);
        return success ? ResponseEntity.ok().build() : ResponseEntity.badRequest().build();}

}

